package com.tiantian.quanminxiu.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.ImageSpan;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.model.ChatMessage;
import com.tiantian.quanminxiu.utils.APPUtils;

import java.util.Arrays;
import java.util.List;

/**
 * Created by admin on 2016/9/7.
 */
public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final int TYPE_1 = 1;//通知语句
    private final int TYPE_2 = 2;//进房信息处理
    private final int TYPE_3 = 3;//公共聊天
    private final int TYPE_4 = 4;//送礼物处理
    private final int TYPE_5 = 4;//充值信息

    private List<ChatMessage> list;
    private Activity activity;

    public ChatAdapter(Activity activity,List<ChatMessage>list){
        this.activity = activity;
        this.list = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        switch (viewType){
            case TYPE_1:
                holder = new NotifyViewHolder(LayoutInflater.from(
                        activity).inflate(R.layout.chatadapter_item_notify, parent,
                        false));
                break;
            case TYPE_3:
                holder = new ChatViewHolder(LayoutInflater.from(
                        activity).inflate(R.layout.chatadapter_item_chat, parent,
                        false));
                break;
            case TYPE_4:
                holder = new GiftViewHolder(LayoutInflater.from(
                        activity).inflate(R.layout.chatadapter_item_gift, parent,
                        false));
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ChatMessage chatMessage = list.get(position);
        switch (getItemViewType(position)){
            case TYPE_1:
                NotifyViewHolder notifyViewHolder = (NotifyViewHolder) holder;
                notifyViewHolder.txtNotify.setText(chatMessage.getWelcomeString());
                break;
            case TYPE_3:
                ChatViewHolder chatViewHolder = (ChatViewHolder) holder;
                chatViewHolder.txtUserContent.setGravity(Gravity.CENTER_VERTICAL);
                SpannableString spannableString = new SpannableString("  "+chatMessage.getUsername()+" "+ chatMessage.getMsgContent());
                spannableString.setSpan(new MyImageSpan(activity,APPUtils.getLevelPic(activity,chatMessage.getUserlLevel())),0,1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#f5a623")),2,chatMessage.getUsername().length()+2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
                chatViewHolder.txtUserContent.setText(spannableString);
                break;
            case TYPE_4:
                GiftViewHolder giftViewHolder = (GiftViewHolder) holder;
                giftViewHolder.txtUserName.setText("[level"+chatMessage.getUserlLevel()+"] "+chatMessage.getUsername());
                giftViewHolder.TxtReceiverName.setText(chatMessage.getGiftName());
                giftViewHolder.txtGiftNum.setText(chatMessage.getGiftCount()+"");

                break;
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    //region view类型 getItemViewType
    @Override
    public int getItemViewType(int position) {
        switch (list.get(position).getType()){
            case 1:
                return TYPE_1;
            case 2:
                return TYPE_2;
            case 3:
                return TYPE_3;
            case 4:
                return TYPE_4;


        }
        return -1;
    }
    //endregion


    public void addMessage(ChatMessage message) {
        list.addAll(Arrays.asList(message));
    }

    public int getMessageSize(){
        return list.size();
    }

    class ChatViewHolder extends RecyclerView.ViewHolder {

        ImageView imgUserLevel;
        TextView txtUserName;
        TextView txtUserContent;

        public ChatViewHolder(View view) {
            super(view);
            imgUserLevel = (ImageView) view.findViewById(R.id.imgUserLevel);
            txtUserName = (TextView) view.findViewById(R.id.txtUserName);
            txtUserContent = (TextView) view.findViewById(R.id.txtUserContent);
        }
    }
    class NotifyViewHolder extends RecyclerView.ViewHolder {

        TextView txtNotify;

        public NotifyViewHolder(View view) {
            super(view);
            txtNotify = (TextView) view.findViewById(R.id.txtNotify);
        }
    }
    class GiftViewHolder extends RecyclerView.ViewHolder {

        TextView txtUserName;
        TextView TxtReceiverName;
        TextView txtGiftNum;
        ImageView imgGift;

        public GiftViewHolder(View view) {
            super(view);
            txtUserName = (TextView) view.findViewById(R.id.txtUserName);
            TxtReceiverName = (TextView) view.findViewById(R.id.TxtReceiverName);
            txtGiftNum = (TextView) view.findViewById(R.id.txtGiftNum);
            imgGift = (ImageView) view.findViewById(R.id.imgGift);
        }
    }

    public class MyImageSpan extends ImageSpan
    {
        public MyImageSpan(Context arg0,int arg1) {
            super(arg0, arg1);
        }
        public int getSize(Paint paint, CharSequence text, int start, int end,
                           Paint.FontMetricsInt fm) {
            Drawable d = getDrawable();
            Rect rect = d.getBounds();
            if (fm != null) {
                Paint.FontMetricsInt fmPaint=paint.getFontMetricsInt();
                int fontHeight = fmPaint.bottom - fmPaint.top;
                int drHeight=rect.bottom-rect.top;

                int top= drHeight/2 - fontHeight/4;
                int bottom=drHeight/2 + fontHeight/4;

                fm.ascent=-bottom;
                fm.top=-bottom;
                fm.bottom=top;
                fm.descent=top;
            }
            return rect.right;
        }

        @Override
        public void draw(Canvas canvas, CharSequence text, int start, int end,
                         float x, int top, int y, int bottom, Paint paint) {
            Drawable b = getDrawable();
            canvas.save();
            int transY = 0;
            transY = ((bottom-top) - b.getBounds().bottom)/2+top;
            canvas.translate(x, transY);
            b.draw(canvas);
            canvas.restore();
        }
    }
}
